import { Injectable } from '@nestjs/common'
import { UserService } from '../user/user.service'
import { InjectRedis } from '@nestjs-modules/ioredis'
import Redis from 'ioredis'
import { CAPTCHA_IMG_KEY } from 'src/common/constant/redis.constant'
import { isEmpty } from 'rxjs'

@Injectable()
export class AuthService {
  constructor(
    private readonly userService: UserService,
    @InjectRedis() private readonly redis: Redis,
  ) {}
  async validateUser(username: string, pass: string): Promise<any> {
    const user = await this.userService.findOne(username)
    if (user && user.password === pass) {
      const { password, ...result } = user
      return result
    }
    return null
  }
  async checkImgCatptcha(uuid: string, code: string) {
    const result = await this.redis.get(`${CAPTCHA_IMG_KEY}:${uuid}`)
    if (!result || code.toLowerCase() !== result.toLowerCase()) {
      throw new Error('验证码不正确')
    }
    await this.redis.del(`${CAPTCHA_IMG_KEY}:${uuid}`)
  }
}
